/**
 * Created by Administrator on 2017/3/28.
 */
$(function(){
    //监听滚动条事件，显示或隐藏回到顶部按钮
    $(window).scroll(function(){
        var $that = $(this);
        if($that.scrollTop()>800){
            $(".sideBar .backTop").show().click(function(){
                $("html,body").stop().animate({ scrollTop: 0 },600);
            });
        }else{
            $(".sideBar .backTop").hide();
        }
    });

    //搜索栏条件选择
    var indextype =[
        {"value":1,"text":"全文"},
        {"value":2,"text":"职位"},
        {"value":3,"text":"公司"}
    ]
    //indextype.option.unshift({"value":8,"text":"请选","selected":true})
    $("#keyword_type").select({
        optionData:indextype,
        css:'indexOptions',
        //selectWidth: 300,//菜单按扭的宽
        //selectHeight: 80,//菜单按扭的高
        offset:Array(-2,-1),
        callback:function(){
            $('.logoAndSearch .searchCont .select i').removeClass('active');
        }
    });

    //搜索栏更多搜索选项
    $(".logoAndSearch .others div a").on("mouseenter",function(){
        $(this).next().show().on("mouseleave",function(){
            $(this).hide();
        });
        $(this).on("mouseleave",function(){
            $(this).next().hide().on("mouseenter",function(){
                $(this).show();
            });
        });
    });

    //导航栏高亮
    var pathName = window.location.pathname; //返回当前页面的路径和文件名
    var navClassNameArr = ["/","/job","/jobinfo","/personnel","/resume","campusrecruit","job_fair","jianzhi","/news","/newsinfo"]; //创建页面路径名数组
    var index; //导航按钮下标
    if(pathName.length == 1){
        $('.mainNav ul li a').eq(0).addClass('active');
    }else{
        for(var i = 1; i < 11; i++){ //获取导航栏下标
            if(pathName.indexOf(navClassNameArr[i]) > -1){
                index = i;
            }
        }
        if(index == 1 || index == 2 ){
            //对应找工作按钮高亮
            $('.mainNav ul li a').eq(1).addClass('active');
        }else if(index == 3 || index == 4){
            //对应找人才按钮高亮
            $('.mainNav ul li a').eq(2).addClass('active');
        }else if(index == 5){
            //对应校园招聘按钮高亮
            $('.mainNav ul li a').eq(3).addClass('active');
        }else if(index == 6){
            //对应招聘会按钮高亮
            $('.mainNav ul li a').eq(5).addClass('active');
        }else if(index == 7){
            //对应兼职招聘按钮高亮
            $('.mainNav ul li a').eq(4).addClass('active');
        }else if(index == 8 || index == 9){
            //对应职场资讯按钮高亮
            $('.mainNav ul li a').eq(7).addClass('active');
        }
    }

    //筛选栏其他选项中下拉框点击事件
    $('.filter .others .cont .select').on('click',function(){
        $(this).find('i').toggleClass('active');
    });
});

/* jquery添加select option */
function jqueryAddOption(obj,arr,othisobj){
    $.each(arr, function(key, value) {
        $('#'+othisobj).find("#"+obj).append($("<option/>", {
            value: value['value'],
            text: value['text']
        }));
    });
}

//数组去重
function array_merge(thisarray){
    var h = {};    //定义一个hash表
    var arr = [];  //定义一个临时数组

    for(var i = 0; i < thisarray.length; i++){    //循环遍历当前数组
        //对元素进行判断，看是否已经存在表中，如果存在则跳过，否则存入临时数组
        if(!h[thisarray[i]]){
            //存入hash表
            h[thisarray[i]] = true;
            //把当前数组元素存入到临时数组中
            arr.push(thisarray[i]);
        }
    }
    return arr;
}

//判断数组是不是存在
function in_array(search,array){
    for(var i in array){
        if(array[i]==search){
            return true;
        }
    }
    return false;
}

//截取中文
function mSubstr(str,size){
    var totalCount = 0;
    var newStr = "";
    for (var i=0; i<str.length; i++) {
        var c = str.charCodeAt(i);
        if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {
            totalCount++;
        }else {
            totalCount+=2;
        }
        if(totalCount<size){
            newStr = str.substring(0,i+1);
        }else{
            return newStr+"...";
        }
    }
    return newStr;
}

/**去除2边函数**/
function str_trim(str){ //删除左右两端的空格
    return str.replace(/(^[\s\,\-\+\，\/\\]*)|([\s\,\-\+\，\/\\]*$)/g, "");
}

//用正则表达式获取地址栏参数
function GetQueryString(name) {
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}

//点击获取手机验证码
var mobTime;
function getModCode(othis,objMobile){
    if($.trim(objMobile.val()) != ''  ){
        var mobValue = $.trim(objMobile.val());
        var regu =/(^1[3-9]{1}[0-9]{9}$)/;
        if(regu.test(mobValue)){
            imgCode = imgCode + Math.random();
            var src= 'this.src="'+imgCode+'" + Math.random()';
            var docSrc = 'document.getElementById("imgCodes").src="'+imgCode+'" + Math.random()';
            var str = "";
            str += "<div class='addCode'>";
            str += "<form method='post' name='mForm' id='mForm' onsubmit='return false'>";
            str += "<span><input name='code' id='code' type='text'  placeholder='请输入验证码' class='code' ></span>";
            str += "<span><img id='imgCodes' src='" + imgCode + "' onclick='"+ src + "' /></span>";
            str += "<span class='change'><a onclick='"+ docSrc + "'>换一换</a></span>";
            str += "<div class='codeErr' id='codeErr'></div>";
            str += "</form>";
            str += "</div>";
            //询问框
            layer.confirm('请输入验证码？', {
                title:'请输入验证码',
                area: ['350px'],
                content: str,
                btn: ['确认','取消'] //按钮
            }, function(){
                var code = $("#mForm").find("#code").val();
                $.post(codeUrl,{'_token':_token,'code':code},function(data) {
                    if(data == 'true'){
                        $.post(mobCodeUrl,{'_token':_token,'mobile':mobValue,'code':code},function(data){
                            if(data.status==2){
                                //验证手机号码是不是存在
                                layer.msg(data.msg, {icon: 5});
                            }else if(data.status==1){
                                //获取验证码成功
                                layer.msg(data.msg, {icon: 6});
                                var iNum=60;
                                mobTime = setInterval(function(){
                                    iNum -= 1;
                                    othis.attr("disabled",true).val('('+iNum+')秒后重新获取').css({"background":"#ccc","color":"#333"});
                                    if(iNum <=0){
                                        othis.removeAttr("disabled").val('点击获取验证码').css({"background":"#465364","color":"#fff"});
                                        clearInterval(mobTime);
                                    }
                                },1000);
                            }else if(data.status==0){
                                //验证码获取失败
                                layer.msg(data.msg, {icon: 5});
                            }
                        });
                    }else{
                        $("#mForm").find("#codeErr").html('验证码错误');
                    }
                });
            });
        }else{
            layer.msg('手机号码错误，不能获取', {icon: 5});
        }
    }else{
        objMobile.val($.trim(objMobile.val())) ;
        layer.msg('请填写手机号码', {icon: 5});
    }
}

//input的placeholder属性在ie7-9的兼容处理
$(function(){

    //判断浏览器是否支持placeholder属性
    var supportPlaceholder='placeholder'in document.createElement('input');

    var placeholder = function(input){

        var text = input.attr('placeholder'),
            defaultValue = input.defaultValue ? input.defaultValue : input.val();

        if(!defaultValue){

            input.val(text).addClass("phcolor");
        }

        input.focus(function(){

            if(input.val() == text){

                $(this).val("");
            }
        });

        input.blur(function(){

            if(input.val() == ""){

                $(this).val(text).addClass("phcolor");
            }
        });

        //输入的字符不为灰色
        input.keydown(function(){

            $(this).removeClass("phcolor");
        });
    };

    //当浏览器不支持placeholder属性时，调用placeholder函数
    if(!supportPlaceholder){

        $('input').each(function(){

            var text = $(this).attr("placeholder");

            if($(this).attr("type") == "text"){

                placeholder($(this));
            }
        });
    }

});

/**
 * 防重复提交
 * @param othis
 * @param form
 */
var formSubmit_nav_name='';
function formSubmit(othis,form){
    form.submit();
    $(othis).attr('disabled', 'disabled');
    formSubmit_nav_name= $(othis).val();
    function settime(countdown){
        if(countdown==0){
            $(othis).attr('disabled',false);
            $(othis).val(formSubmit_nav_name);
        }else{
            $(othis).val("请稍候"+countdown+"...");
            countdown--;
            setTimeout(function(){
                settime(countdown);
            },1000);
        }
    }
    settime(5);
}


/**
 * 检查提交表单重复插入
 * @param form
 * @returns {boolean}
 */
function checkForm(form){
    $(form).find("input[type='submit']").val('请稍候');
    $(form).find("input[type='submit']").attr('disabled', true);
    $(form).find("input[type='submit']").attr('readonly', true);
    return true;
}

/**
 * 设置一下cookie
 * @param name 名称
 * @param value 值
 * @param days 保存的时间null代表关闭失效
 */
function setCookie(name, value, days){
    if(days == null){
        document.cookie = name + "=" + value + ";path=/";
    }else{
        var exp = new Date();
        exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
        document.cookie = name + "=" + value + ";path=/" + ";expires=" + exp.toGMTString();
    }
}

/**
 * 获取一个cookie
 * @param name cookie名称
 * @returns {*}
 */
function getCookie(name){
    var strArg = name + "=";
    var nArgLen = strArg.length;
    var nCookieLen = document.cookie.length;
    var nEnd;
    var i = 0;
    var j;
    while(i < nCookieLen){
        j = i + nArgLen;
        if(document.cookie.substring(i, j) == strArg){
            nEnd = document.cookie.indexOf(";", j);
            if(nEnd == -1) nEnd = document.cookie.length;
            //return decodeURIComponent(document.cookie.substring(j,nEnd));
            return document.cookie.substring(j, nEnd);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if(i == 0) break;
    }
    return null;
}